package cn.afterturn.easypdf.pdf.handler.impl;

import cn.afterturn.easypdf.pdf.entity.PdfExportParams;
import cn.afterturn.easypdf.pdf.handler.IPdfFontHandler;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

import java.io.File;
import java.io.IOException;

public class PdfFontHandlerDefaultImpl implements IPdfFontHandler {

    private PDFont font;
    private PDFont boldFont;

    @Override
    public PDFont getTitleFont(PDDocument document, PdfExportParams params) throws IOException {
        return getContentFont(document, params);
    }

    @Override
    public PDFont getContentFont(PDDocument document, PdfExportParams params) throws IOException {
        if (font == null) {
            font = PDType0Font.load(document, new File(getSysFont()));
        }
        return font;
    }

    @Override
    public PDFont getBoldContentFont(PDDocument document, PdfExportParams params) throws IOException {
        if (boldFont == null) {
            boldFont = PDType0Font.load(document, new File(getSysBoldFont()));
        }
        return boldFont;
    }

    private String getSysFont() {
        return PdfFontHandlerDefaultImpl.class.getClassLoader().getResource("fonts/simfang.ttf").getFile();
    }

    private String getSysBoldFont() {
        return PdfFontHandlerDefaultImpl.class.getClassLoader().getResource("fonts/simhei.ttf").getFile();
    }
}
